function number_format(number, decimals, dec_point, thousands_sep) {          
  number = (number + '')
    .replace(/[^0-9+\-Ee.]/g, '');
  var n = !isFinite(+number) ? 0 : +number,
    prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
    sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
    dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
    s = '',
    toFixedFix = function(n, prec) {
      var k = Math.pow(10, prec);
      return '' + (Math.round(n * k) / k)
        .toFixed(prec);
    };
  // Fix for IE parseFloat(0.55).toFixed(0) = 0;
  s = (prec ? toFixedFix(n, prec) : '' + Math.round(n))
    .split('.');
  if (s[0].length > 3) {
    s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
  }
  if ((s[1] || '')
    .length < prec) {
    s[1] = s[1] || '';
    s[1] += new Array(prec - s[1].length + 1)
      .join('0');
  }
  return s.join(dec)+' ₫';
}

$(document).ready(function() {
   //nut dang ky email
   $('.reg_submit').click(function(){
      var reg_email = $('.reg_email').val();
      if(!isemail(reg_email)) {
         alert('Vui lòng nhập 1 email hợp lệ');
         return false;
      }else {
         var data = {name:'regemail',reg_email:reg_email};
         loadAjax(data,'json',{
            success:function(res) {
               alert(res.html);
            }
         });
         return false;
      }
   });
   $('.action_slide_image').click(function(){
      $('.prodetail_slide_item').css({'display':'none'});
      $('.prodetail_slide_image').css({'display':'block'});
      $('.prodetail_slide_control_item .thums').removeClass('active');
      $(this).addClass('active');
   });
   $('.action_slide_video').click(function(){
      $('.prodetail_slide_item').css({'display':'none'});
      $('.prodetail_slide_video').css({'display':'block'});
      $('.prodetail_slide_control_item .thums').removeClass('active');
      $(this).addClass('active');
   });
   $('.thumvideo').click(function(){
      $('.thumvideo').removeClass('active');
      $(this).addClass('active');
      var claction = $(this).attr('data-cl');
      var videoid = $(this).attr('data-link');
      $('.cont').css({'display':'none'});
      $('.'+claction).css({'display':'block'});
      var content_ac = $('.'+claction).html();
      if(content_ac.trim() == '') {
         $('.'+claction).html('<iframe width="100%" height="100%" src="http://www.youtube.com/embed/'+videoid+'?rel=0" frameborder="0" allowfullscreen=""></iframe>');
      }
   });
   //nut đổi màu sản phẩm
   $('.color_item').click(function(){
      $('.color_item').removeClass('active');
      $(this).addClass('active');
      var product_price = $('.pro_price').attr('data-price');
      if(product_price == 68000000)
         return;
      var color_price = $(this).attr('data-price');
      var gold_price = $('.pro_goldwarranty').attr('data-price');
      if(!$(".goldwarranty").is(':checked')) {
         gold_price = 0;
      }
      var curr_price = parseInt(color_price) + parseInt(gold_price);
      $('.pro_price').html(number_format(curr_price,0,',','.'));
      $('.pro_price').attr('data-price',curr_price);
   });
   //nut bảo hành vàng
   $('.goldwarranty').click(function(){
      var product_price = $('.pro_price').attr('data-price');
      if(product_price == 68000000)
         return;
      var gold_price = $('.pro_goldwarranty').attr('data-price');
      var color_price = $('.color_item.active').attr('data-price');
      if(color_price == null) {
         color_price = product_price;
      }
      if($(".goldwarranty").is(':checked')) {
         var curr_price = parseInt(color_price) + parseInt(gold_price);
      }else {
         var curr_price = parseInt(color_price);
      }
      $('.pro_price').html(number_format(curr_price,0,',','.'));
   });
   //nut xem thong so chi tiet
   $('.show_param').click(function(){
      $('.tableparamexpan').toggle('slow');
   });
   //nut dong thong so chi tiet
   $('.btnparam2').click(function(){
      $('.tableparamexpan').toggle('slow');
   });
   //nut show chi tiet bai viet
   $('.click-show-more').click(function(){
      if($('.prodetail_content_wrap').hasClass('expand')) {
         $('.prodetail_content_wrap').removeClass('expand');
         $('.click-show-more').html('Xem thêm đánh giá chi tiết ▼');
      }else {
         $('.prodetail_content_wrap').addClass('expand');
         $('.click-show-more').html('Thu gọn đánh giá chi tiết ▲');
      }
   });
   
   //Thanh menu cuon
   barH = $('.bar').outerHeight();
   $(window).scroll(function(){
      var fromTop = $(this).scrollTop()+barH;
      if(fromTop < 131){
      $('.bar').removeClass('fix_bar');
      }else{ 
      $('.bar').addClass('fix_bar');
      $('.bar').css({'top':'0'});	
      }               
   }); 
})